<?php
//自动生成,请根据需要修改
namespace app\login\controller;
use app\common\controller\Base;
use think\facade\Request;
use think\Db;
/**
* @title 账号登录凭证接口
* @description 账号登录凭证说明
* @group 登录
*/
class Account extends Base{

	/**
	* @title 新增账号（登录名、密码）
	* @description 新增账号（登录名、密码）
	* @author 开发者（宋晓文）
	* @url /login/account/add
	* @method post
	*
	* @param name:token require:1 default: desc:token
	* @param name:account_name type:VarChar require:1 default:0 other: desc:登录名
	* @param name:password type:VarChar require:1 default:0 other: desc:密码
	*
	* @return account_name:登录名
	* @return password:密码
	* @return openid:
	* @return islock:是否锁定
	*/
	public function add (){
		//模块 - 输入数据
		$input['account_name'] = input('account_name');
		$input['password'] = input('password');

		//模块 - 验证器
		$accountValidate = new \app\login\validate\Account;
		$validate = $accountValidate->scene('add')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $accountValidate->getError();
			$msg['data'] = $input;
			return $this->errorJson($msg);
		}

        $check_repeat = Db::name('account')->where('account_name',$input['account_name'])->find();
		if ($check_repeat){
            $msg['msg'] = '该登录名已存在';
            return $this->errorJson($msg);
        }

		//插入的数据
		$insert_data = [
			'account_name'=>$input['account_name'],
			'password'=>$input['password'],
		];
		//添加数据
		$insert_res = Db::name('account')->insertGetId($insert_data);
        //接口返回
		if(!$insert_res){
			$msg['msg'] = '添加账号失败';
			return $this->errorJson($msg);
		}else{
            $msg['account_id'] = $insert_res;
            $msg['msg'] = '操作成功';
            return $this->successJson($msg);
        }

	}
	/**
	* @title 新增账号（微信）
	* @description 新增账号（微信）
	* @author 开发者（宋晓文）
	* @url /login/account/add_by_mini
	* @method post
	*
	* @param name:token require:1 default: desc:token
	* @param name:openid type:VarChar require:1 default:0 other: desc:
	*
	* @return account_id:账号id
	*/
	public function add_by_mini (){
		//模块 - 输入数据
		$input['openId'] = input('openId');

		//模块 - 验证器
		$accountValidate = new \app\login\validate\Account;
		$validate = $accountValidate->scene('add_by_mini')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $accountValidate->getError();
			$msg['data'] = $input;
			return $this->errorJson($msg);
		}

        $check_repeat = Db::name('account')->where('openId',$input['openId'])->find();
		if ($check_repeat){
            $msg['msg'] = '该登录名已存在';
            return $this->errorJson($msg);
        }

		//插入的数据
		$insert_data = [
			'openId'=>$input['openId'],
		];
		//添加数据
		$insert_res = Db::name('account')->insertGetId($insert_data);
        //接口返回
		if(!$insert_res){
			$msg['msg'] = '添加账号失败';
			return $this->errorJson($msg);
		}else{
            $msg['account_id'] = $insert_res;
            $msg['msg'] = '操作成功';
            return $this->successJson($msg);
        }

	}
	/**
	* @title 获取单条数据
	* @description 根据ID获取账号登录凭证的指定记录
	* @author 开发者（宋晓文）
	* @url /login/account/get_name_by_account_id
	* @method *
	* @param name:token require:1 default: desc:token
	* @param name:id type:Int require:1 default:0 other: desc:
	*
	* @return id:
	*/
	public function get_name_by_account_id (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/s');
		//模块 - 验证器
		$accountValidate = new \app\login\validate\Account;
		$validate = $accountValidate->scene('only_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $accountValidate->getError();
			return $this->errorJson($msg);
		}
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
		];
		//去除为空的条件
		$find_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($find_map,$v);
		}
		//获取数据
		$select_res = Db::name('account')->where($find_map)->find();
		// find 返回数据
		$msg['data']['id'] = $select_res['id'];
		$msg['data']['account_name'] = $select_res['account_name'];

		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
    /**
     * @title 获取帐号列表
     * @description 获取帐号列表
     * @author 开发者（宋晓文）
     * @url /login/account/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:id type:String require:1 default:0 other: 多个帐号可以用逗号隔开:
     * @param name:account_name type:String require:1 default:0 other: account_name:
     * @param name:openId type:String require:1 default:0 other: openId:
     * @param name:islock type:String require:1 default:0 other: islock:
     *
     * @return data:
     */
    public function get_list (){
        //模块 - 输入数据
        $model = new \app\login\model\Account();
        $Fields = $model->getTableFields();
        $input = \think\facade\Request::only($Fields);
        //查询条件
        //去除为空的条件
        $find_map =$input;
        if(isset($find_map['id'])){
            $find_map['id']=explode(",",$find_map['id']);
        }
        //获取数据
        $data = Db::name('account')->where($find_map)->select();
        // find 返回数据
        $msg['data'] =$data;
        //接口输出json数据
        $msg['msg'] = '操作成功！';
        $msg['sql'] =  Db::name('account')->getLastSql();
        return $this->successJson($msg);
    }
}
